package com.gitee.LiChenLiang.SpringSecurityDemo.configurer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    // HTTP请求安全处理
    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        http
//            .authorizeRequests()
//                // 配置任何用户都可访问的路径
//                .antMatchers("/resources/**", "/signup", "/about").permitAll()
//                // 尚未匹配的任何URL都要求用户进行身份验证
//                .anyRequest().authenticated()
//                .and()
//            // 登录方式配置
//            .formLogin();

        http
            .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("admin").password("admin").roles("USER");
    }

    // 身份验证管理生成器
//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
////        super.configure(auth);
//        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
//    }

    // WEB安全
//    @Override
//    public void configure(WebSecurity web) throws Exception {
//        super.configure(web);
//    }

}
